<?php
/**
 * 文章管理
 */
namespace Admin\Controller;
use Admin\Controller\AdminController;
class ArticleController extends AdminController {

	//所有文章
    public function index(){
        $data = D("Article")->getArticles(10,array("trash"=>0),"id desc");
        $this->article = $data['article']; //文章列表
        $this->page    = $data['page'];   //分页导航
        $this->assign("title","文章列表");
        $this->display();
    }
    //添加文章
    public function add(){
        $cate = M('category')->field("id,pid,name,slug")->select();;
        $this->assign('cate',get_sort_cate($cate));          
    	$this->assign("title","添加文章");
        $this->display("add");
    }
    //添加文章处理表单
    public function addHandle(){ 
        if(!IS_POST)$this->error("非法请求!");
        $Article = D('article');
        $arr = I();
        $arr['user_id'] = session('userid');
        $arr['date'] = time();
        if($arr['top']) $arr['top'] = 1;
        if (!$Article->create()) $this->error($Article->getError());
        $result = $Article->add($arr);
        $result?$this->success("添加成功"):$this->error("添加失败");
    }
    //删除文章
    public function del(){
        $Article = M('article');
        $result = null;
        $total=0;
        $data = array("trash"=>1,"date"=>time());
        if(I('ids')){
            foreach (I('ids') as $id) {
                $result = $Article->where("id=".$id)->setField($data);
                $total++;
            }
        }else{
            $result = $Article->where("id=".I('id'))->setField($data);
            $total=1;
        }
        $result?$this->success('成功移动<span style="color:red">'.$total.'</span>篇文章至回收站'):$this->error("移动失败");
    }
    //编辑文章
    public function edit(){
        $cate = M('category')->field("id,pid,name,slug")->select();;
        $this->assign('cate',get_sort_cate($cate));  
        $this->article = M('article')
                         ->where("id=".I('id'))
                         ->field("date,user_id,trash",true)
                         ->select();
        $this->display();
    }
    //编辑文章处理表单
    public function editHandle(){
        $data = I();
        if($data['top']) $data['top'] = 1;
        $data['date'] = time();
        $result = M('article')->save($data);
        $result?$this->success("文章更新成功"):$this->error("文章更新失败");
    }
    //回收站
    public function trash(){
        $data = D("Article")->getArticles(10,array("trash"=>1),"date desc");
        $this->article = $data['article']; //文章列表
        $this->page    = $data['page'];   //分页导航
        $this->assign("title","回收站");
        $this->display();
    }
    //清空回收站
    public function clearTrash(){
        $Article = M('article');
        $Article ->delete(I('id'))?$this->success("文章永久删除成功"):$this->error("文章永久删除失败");
    }
    //还原回收站文章
    public function restoreTrash(){
        $Article = M('article');
        $result = null;
        $total = 0;
        $data = array("trash"=>0,"date"=>time());
        if(I('ids')){
            foreach (I('ids') as $id) {
                $result = $Article->where("id=".$id)->setField($data);
                $total++;
            }
        }else{
            $result = $Article->where("id=".I('id'))->setField($data);
            $total=1;
        }
        $result?$this->success('批量还原成功<span style="color:red">'.$total.'</span>篇文章'):$this->error("还原失败");
    }

    //上传缩略图
    public function thumb(){
        $upload = new \Think\Upload();
        $upload->maxSize   =     3145728 ;
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');
        $upload->subName   =    array('date','Ym');
        $upload->rootPath  =     './upload/image/';
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $data['status'] = 0;
            $data['info'] =$upload->getError() ;
            $this->ajaxReturn($data,'JSON');
        }else{// 上传成功
            $path="/upload/image/".$info['photo']['savepath'].$info['photo']['savename'];
            $data['status'] = 1;
            $data['info'] = "上传成功";
            $data['path'] = $path;
            $this->ajaxReturn($data,'JSON');
        }
    }
}